﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>培训计划新增</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="weadmin-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="TPName" class="layui-form-label">
                    <span class="we-red">*</span>计划名称
                </label>
                <div class="layui-input-block">
                    <input type="text" id="TPName" name="TPName" placeholder="请输入" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="TPDescribe" class="layui-form-label">
                    计划描述
                </label>
                <div class="layui-input-block">
                    <textarea id="TPDescribe" name="TPDescribe" placeholder="请输入" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">

                <label for="TPReleaseStart" class="layui-form-label">
                    <span class="we-red">*</span>结束时间
                </label>
                <div class="layui-input-inline">
                    <input id="TPReleaseStart" name="TPReleaseStart" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="TPReleaseEnd" class="layui-form-label">
                    <span class="we-red">*</span>结束时间
                </label>
                <div class="layui-input-inline">
                    <input id="TPReleaseEnd" name="TPReleaseEnd" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="TPDepID" class="layui-form-label">
                    <span class="we-red">*</span>考试部门
                </label>
                <div id="TPDepID" class="layui-input-block">
                </div>
            </div>
            <div class="layui-form-item">

                <label for="TPDesignUserName" class="layui-form-label">
                    设计人员
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="TPDesignUserName" name="TPDesignUserName" placeholder="多个人|隔开" autocomplete="off" class="layui-input">
                </div>
                <!--<label for="TPIsRelease" class="layui-form-label">
                    在发布
                </label>
                <div class="layui-input-inline">
                    <input type="checkbox" id="TPIsRelease" name="TPIsRelease" lay-skin="switch" lay-filter="switchIsRelease" lay-text="是|否">
                </div>-->
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="update">修改</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../static/js/common.js" charset="utf-8"></script>

    <script>
        layui.extend({
            selectM: '../../../lib/layui_extends/selectM'
        });
        layui.use(['form', 'jquery', 'layer', 'laydate', 'selectM'], function () {
            var form = layui.form,
                $ = layui.jquery,
                laydate = layui.laydate,
                selectM = layui.selectM,
                layer = layui.layer;
            laydate.render({
                elem: '#TPReleaseStart'
            });
            laydate.render({
                elem: '#TPReleaseEnd'
            });
            $.support.cors = true;
            var depdata = [];
            var deplist = $.ajax({
                url: ApiUrl + "Dep/Sel",
                type: 'post',
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
                success: function (data) {
                    if (data.StatusCode == 200) {
                        depdata = data.Data;
                    }
                    else if (data.StatusCode == 403) {
                        top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                            top.location.href = "http://" + top.location.host + "/login.html";
                        })
                    }
                    else {
                        layer.msg(data.Info);
                    }
                },
                //防止获取失败时，界面不现实下拉元素
                complete: function () {
                    selectM({
                        //元素容器【必填】
                        elem: '#TPDepID',
                        tips: '请选择考试部门',
                        //候选数据【必填】
                        data: depdata,
                        selected: depdata.split(","),
                        field: {
                            idName: 'DepID',
                            titleName: 'DepName'
                        },
                        max: 100,
                        //添加验证
                        verify: 'required'
                    });
                }
            });
            var TPID = getUrlParam("TPID");
            var alldeptid;
            var singlelist = $.ajax({
                url: ApiUrl + "TrainPlan/GetSingle",
                type: 'get',
                dataType: 'json',
                cache: false,
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
                data: { TPID: getUrlParam("TPID") },
                success: function (data) {
                    if (data.StatusCode == 200) {
                        document.getElementById("TPName").value = data.Data.TPName;
                        document.getElementById("TPDescribe").value = data.Data.TPDescribe;
                        document.getElementById("TPReleaseStart").value = data.Data.TPReleaseStart.substring(0, 10);
                        document.getElementById("TPReleaseEnd").value = data.Data.TPReleaseEnd.substring(0, 10);
                        document.getElementById("TPDesignUserName").value = data.Data.TPDesignUserName;
                        //document.getElementById("TPIsRelease").value = data.Data.TPIsRelease == 0 ? false : true;
                        alldeptid = data.Data.TPDepID;
                    }
                    else if (data.StatusCode == 403) {
                        top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                            top.location.href = "http://" + top.location.host + "/login.html";
                        })
                    }
                    else {
                        layer.msg(data.Info);
                    }
                }
            });
            //相关ajax加载完成后，绑定数据
            $.when(deplist, singlelist)
                .done(function () {


                    selectM({
                        //元素容器【必填】
                        elem: '#TPDepID',
                        tips: '请选择考试部门',
                        //候选数据【必填】
                        data: depdata,
                        selected: alldeptid.split(','),
                        field: {
                            idName: 'DepID',
                            titleName: 'DepName'
                        },
                        max: 100,
                        //添加验证
                        verify: 'required'
                    });
                    
                    form.render();
                })
                .fail(function () {
                });

            //监听修改
            form.on('submit(update)', function (formdata) {
                //console.log(formdata.field)
                //处理开关返回值，也可通过设置value='1'让选中时返回的on改为1，但未选中时，仍不返回值，需要model接收时使用int?得到默认值0，因此在此处理比较安全
                formdata.field.EPIsRelease = formdata.field.EPIsRelease == "on" ? "1" : "0";
                formdata.field.TPID = TPID;
                $.ajax({
                    url: ApiUrl + "TrainPlan/Edit",
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    data: formdata.field,
                    success: function (data) {
                        console.log(data)
                        if (data.StatusCode == 200) {
                            layer.alert("增加成功", { icon: 6 }, function (index) {
                                parent.layer.closeAll();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>